package com.hoangkien.apdater;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.hoangkien.entity.SoundCloudEntity;
import com.hoangkien.soundcloud.R;
import com.hoangkien.soundcloud.activity.PlayMusicActivity;
import com.hoangkien.util.UniversalImageLoader;

public class AdapterAllSoundCloud extends BaseAdapter{
	
	private Context context;
	private UniversalImageLoader iloader;
	private ArrayList<SoundCloudEntity> mArrayList;
	private LayoutInflater inflater;
	
	
	public AdapterAllSoundCloud(Context context, ArrayList<SoundCloudEntity> mArrayList){
		this.context = context;
		this.mArrayList = mArrayList;
		this.inflater = LayoutInflater.from(context);
		this.iloader = new UniversalImageLoader(context);
	}

	@Override
	public int getCount() {
		return mArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return getItem(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.adapter_all_layout,parent, false);
			holder = new ViewHolder();
			holder.adapterAllImg = (ImageView) convertView.findViewById(R.id.adapter_all_img);
			holder.adapterAllName = (TextView) convertView.findViewById(R.id.adapter_all_name);
			holder.adapterAllAuthor = (TextView) convertView.findViewById(R.id.adapter_all_author);
			holder.adapterAllPlay = (CheckBox) convertView.findViewById(R.id.adapter_all_play);
			holder.adapterAllTime = (TextView) convertView.findViewById(R.id.adapter_all_time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.adapterAllName.setText(subString(mArrayList.get(position).getTitle()));
		holder.adapterAllTime.setText(mArrayList.get(position).getDuration()+"");
		holder.adapterAllPlay.setText(mArrayList.get(position).getPlayback_count()+"");
		
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				PlayMusicActivity.actionLaunch(context);
			}
		});
		return convertView;
	}
	
	private class ViewHolder {
		private ImageView adapterAllImg;
		private TextView adapterAllName;
		private TextView adapterAllAuthor;
		private CheckBox adapterAllPlay;
		private TextView adapterAllTime;
	}
	
	
	private String subString(String title){
		if(title.length() >= 30 ){
			title = title.substring(0, 28)+"...";
		} else {
			
		}
		return title;
	}

}
